package com.yueqian.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yueqian.framework.entity.BaseEntity;
import com.yueqian.system.entity.Role;
import com.yueqian.system.entity.User;
import com.yueqian.system.entity.UserRole;
import com.yueqian.system.mapper.UserRoleMapper;
import com.yueqian.system.service.RoleService;
import com.yueqian.system.service.SystemUserService;
import com.yueqian.system.service.UserRoleService;
import com.yueqian.system.vo.req.UserRoleReqVO;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {


	@Resource
	private UserRoleMapper userRoleMapper;

	@Resource
	@Lazy
	private SystemUserService systemUserService;


	@Resource
	private RoleService roleService;

	public Page<User> selectPageData(UserRoleReqVO userRoleReqVO) {
		IPage<UserRole> userRoleIPage = userRoleReqVO.getPage(userRoleReqVO.getCurrentPage(), userRoleReqVO.getPageSize());
		return userRoleMapper.selectPageData(userRoleIPage, userRoleReqVO);
	}

	@Override
	public void delete(List<Long> userIds, Long roleId) {
		userIds.forEach(userId -> {
			UserRole userRole = this.baseMapper.selectOne(Wrappers.lambdaQuery(UserRole.class).eq(ObjectUtil.isNotEmpty(userId), UserRole::getUserId, userId)
																  .eq(ObjectUtil.isNotEmpty(roleId), UserRole::getRoleId, roleId));
			if (ObjectUtil.isNotEmpty(userRole)) {
				this.removeById(userRole.getId());
			}
		});
	}

	/**
	 * 角色分配用户
	 *
	 * @param userIds
	 * @param roleId
	 */
	@Override
	public Boolean add(List<Long> userIds, Long roleId) {
		if (CollectionUtil.isEmpty(userIds)) {
			return false;
		}
		userIds.forEach(userId -> {
			List<UserRole> userRoles = this.baseMapper.selectList(Wrappers.lambdaQuery(UserRole.class).eq(ObjectUtil.isNotEmpty(userId), UserRole::getUserId, userId)
																		  .eq(ObjectUtil.isNotEmpty(roleId), UserRole::getRoleId, roleId));
			if (CollectionUtil.isEmpty(userRoles)) {
				UserRole userRole = new UserRole();
				userRole.setRoleId(roleId);
				userRole.setUserId(userId);
				this.saveOrUpdate(userRole);
			}
		});
		return true;
	}

	/**
	 * 用户分配角色
	 *
	 * @param userId
	 * @param roleIds
	 */
	@Override
	@Transactional
	public Boolean add(Long userId, List<Long> roleIds) {
		User user = systemUserService.getById(userId);
		if (ObjectUtil.isEmpty(user)) {
			throw new RuntimeException("用户不存在");
		}

		//先删除之前存在的用户角色
		List<Long> ids = this.list(Wrappers.lambdaQuery(UserRole.class).eq(UserRole::getUserId, userId)).stream().map(BaseEntity::getId).collect(Collectors.toList());
		this.removeByIds(ids);
		for (Long roleId : roleIds) {
			Role role = roleService.getById(roleId);
			if (ObjectUtil.isEmpty(role)) {
				throw new RuntimeException("角色不存在");
			}
			UserRole userRole = new UserRole();
			userRole.setUserId(userId);
			userRole.setRoleId(roleId);
			//后保存新增的
			this.saveOrUpdate(userRole);
		}
		return true;
	}

	@Override
	public List<UserRole> selectListData(UserRoleReqVO reqVO) {
		return this.list(Wrappers.lambdaQuery(UserRole.class).eq(ObjectUtil.isNotEmpty(reqVO.getUserId()), UserRole::getUserId, reqVO.getUserId()));
	}
}
